package com.group15.phms;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.group15.phms.DeleteDialogFragment.DeleteDialogListener;

import android.os.Bundle;
import android.app.DialogFragment;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;
import android.support.v4.app.NavUtils;

public class Notes extends ListActivity implements DeleteDialogListener{
	SharedPreferences logins;
	DatabaseHelper db;
	ArrayAdapter<String> adapter;
	String currentUser;
	List<String> notes;
	ListView notesList;
	String deleteTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_notes);
		// Show the Up button in the action bar.
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		logins = getSharedPreferences("logins",MODE_PRIVATE);
		currentUser = logins.getString("currentUser","INVALID");
		db= new DatabaseHelper(getApplicationContext());
		notes = db.getNoteTitles(currentUser);
		Collections.sort(notes, new NoteComparator());
		notes.add("<Tap to add new note>");
		adapter = new ArrayAdapter<String>(  this, 
                android.R.layout.simple_list_item_1,
                notes
            );
		notesList=(ListView)findViewById(android.R.id.list);
		notesList.setAdapter(adapter);
		notesList.setEmptyView(findViewById(R.id.empty));
		db.closeDB();
		
		this.getListView().setLongClickable(true);
		this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
		    public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
		    	DialogFragment f = new DeleteDialogFragment();
		        f.show(getFragmentManager(), "DeleteDialogFragment");
		        deleteTitle = notes.get(position).toString();
		        return true;
		    }
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.notes, menu);
		return true;
	}
	Intent intent;
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		case R.id.action_logout:
			intent = new Intent(this, Login.class);
	    	startActivity(intent);
	    	finish();
			return true;
		case R.id.addbutton:
			intent = new Intent(this, NoteEditor.class);
	    	startActivity(intent);
			return true;
		case R.id.action_search:
			intent = new Intent(this, Search.class);
	    	startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onRestart()
	{
		super.onRestart();
		db= new DatabaseHelper(getApplicationContext());
		notes = db.getNoteTitles(currentUser);	
		Collections.sort(notes, new NoteComparator());
		notes.add("<Tap to add new note>");
		adapter = new ArrayAdapter<String>(  this, 
                android.R.layout.simple_list_item_1,
                notes
            );
		notesList=(ListView)findViewById(android.R.id.list);
		notesList.setAdapter(adapter);
		notesList.setEmptyView(findViewById(R.id.empty));
		adapter.notifyDataSetChanged();
		db.closeDB();
	}
	
	@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
          // Get the item that was clicked
        if(position==l.getCount()-1)
        {
        	Intent intent = new Intent(this, NoteEditor.class);
            startActivity(intent);
        }
        else if(position!=l.getCount()-1)
        {
	        String title = this.getListView().getItemAtPosition(position).toString();
	        Intent intent = new Intent(this, NoteEditor.class);
	        intent.putExtra("title", title);
	        startActivity(intent); 
        }
	}

	@Override
	public void onPositiveClick() {
		db= new DatabaseHelper(getApplicationContext());
		db.deleteNote(currentUser, deleteTitle);
		Toast.makeText(getApplicationContext(),"Note deleted successfully", Toast.LENGTH_SHORT).show();
		notes = db.getNoteTitles(currentUser);	
		Collections.sort(notes, new NoteComparator());
		notes.add("<Tap to add new note>");
		adapter = new ArrayAdapter<String>(  this, 
                android.R.layout.simple_list_item_1,
                notes
            );
		notesList=(ListView)findViewById(android.R.id.list);
		notesList.setAdapter(adapter);
		notesList.setEmptyView(findViewById(R.id.empty));
		adapter.notifyDataSetChanged();
	}

}


class NoteComparator implements Comparator<String> {
    @Override
    public int compare(String s1, String s2) {
        return s1.toLowerCase().compareTo(s2.toLowerCase());
    }
}